home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / corprt4.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  321 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* 
  18.  *     corimg4 -
  19.  *        This contains generic corimg4 and sproof4 stuff.  This color
  20.  *    Correction technique is for multi angle screening with 4 colors
  21.  *
  22.  *                Paul Haeberli - 1991
  23.  *
  24.  *    use corone4(r,g,b,cr,cg,cb)
  25.  *    and sproof4(r,g,b,cr,cg,cb)  in the simplest case.
  26.  *
  27.  *    exports
  28.  *
  29.     void corrow4(rbuf,gbuf,bbuf,n)
  30.     void corone4(fr,fg,fb,cr,cg,cb)
  31.     void sproofrow4(rbuf,gbuf,bbuf,n)
  32.     void sproof4(r,g,b,sr,sg,sb)
  33.  *
  34.  */
  35. #include "stdio.h"
  36. #include "image.h"
  37. #include "math.h"
  38. #include "texture.h"
  39. #include "lum.h"
  40. #include "swop.h"
  41.  
  42. /* 
  43.  *    correction stuff follows
  44.  *
  45.  */
  46. #define DESAT 1
  47. #define CORTAB "/usr/lib/cortab.rgb"
  48. #define CORMAG "/usr/lib/cortab.mag"
  49.  
  50. #define EPSILON        (0.0001)
  51. #define FLAGBIT        (1<<14)
  52.  
  53. #define RSCALE        (16383.0*RLUM)
  54. #define GSCALE        (16383.0*GLUM)
  55. #define BSCALE        (16383.0*BLUM)
  56.  
  57. float flerp();
  58.  
  59. static TEXTURE *tm;
  60. static short *sbuf;
  61. static int magxsize;
  62. static int magysize;
  63.  
  64. static int simpcorone(fr,fg,fb,r,g,b)
  65. float fr, fg, fb;
  66. float *r, *g, *b;
  67. {
  68.     int mapno;
  69.     float mag, mapx, mapy;
  70.     int imapx, imapy;
  71.     int tmsize, magtab;
  72.     float bscale;
  73.     vect c;
  74.     
  75. /* fprintf(stderr,"rgb is %f %f %f\n",fr,fg,fb); */
  76.     if(!tm) {
  77.     IMAGE *simage;
  78.     int y;
  79.  
  80.     tm = tmopen(CORTAB);
  81.     if(!tm) {
  82.         fprintf(stderr,"corimg: can't open %s\n",CORTAB);
  83.         exit(1);
  84.     }
  85.     simage = iopen(CORMAG,"r");
  86.     if(!simage) {
  87.         fprintf(stderr,"corimg: can't open %s\n",CORMAG);
  88.         exit(1);
  89.     }
  90.     magxsize = simage->xsize;
  91.     magysize = simage->ysize;
  92.     sbuf = (short *)malloc(simage->xsize*simage->ysize*sizeof(short));
  93.     for(y=0; y<simage->ysize; y++) 
  94.         getrow(simage,sbuf+(y*simage->xsize),y,0);
  95.     iclose(simage);
  96.     }
  97.     tmsize = tm->ysize;
  98.     if(fr<EPSILON && fg<EPSILON && fb<EPSILON) {
  99.     *r = 0.0;
  100.     *g = 0.0;
  101.     *b = 0.0;
  102.     return 1;
  103.     } else {
  104.     if(fr>fg) {
  105.         if(fr>fb) {
  106.         mapno = 0;
  107.         mapx = fg/fr;
  108.         mapy = fb/fr;
  109.         } else {
  110.         mapno = 2;
  111.         mapx = fr/fb;
  112.         mapy = fg/fb;
  113.         }
  114.     } else {
  115.        if(fg>fb) {
  116.            mapno = 1;
  117.            mapx = fb/fg;
  118.            mapy = fr/fg;
  119.        } else {
  120.            mapno = 2;
  121.            mapx = fr/fb;
  122.            mapy = fg/fb;
  123.        }
  124.     }
  125. /* fprintf(stderr,"mapno is %d\n",mapno); */
  126.     imapx = (mapno*tmsize)+mapx*(tmsize-1.0)+0.5;
  127.     imapy = mapy*(tmsize-1.0)+0.5;
  128. /* fprintf(stderr,"maploc is %d %d\n",imapx,imapy); */
  129.     mag = RSCALE*fr+GSCALE*fg+BSCALE*fb;
  130. /* fprintf(stderr,"mag is %f\n",mag); */
  131.     magtab = sbuf[imapy*magxsize+imapx];
  132.     magtab &= ~FLAGBIT;
  133. /* fprintf(stderr,"magtab is %d\n",magtab); */
  134.     bscale = mag/magtab;
  135. /* fprintf(stderr,"bscale is %f\n",bscale); */
  136.     imgsample(tm,imapx,imapy,&c);
  137.     if(bscale <= 1.0) {
  138.         *r = (bscale*c.x);
  139.         *g = (bscale*c.y);
  140.         *b = (bscale*c.z);
  141.         return 1;
  142.     } else {
  143.         bscale = 1.0;
  144.         *r = (bscale*c.x);
  145.         *g = (bscale*c.y);
  146.         *b = (bscale*c.z);
  147.         return 0;
  148.     }
  149.     }
  150. }
  151.  
  152. void corone4(fr,fg,fb,cr,cg,cb)
  153. float fr, fg, fb;
  154. float *cr, *cg, *cb;
  155. {
  156.     float tr, tg, tb;
  157.     float r, g, b, lum;
  158.     float delta, p, goodp;
  159.     int i;
  160.  
  161.     if(DESAT) {
  162.     if(!simpcorone(fr,fg,fb,&r,&g,&b)) {
  163.         lum = LUM(fr,fg,fb);
  164.         p = 0.5;
  165.         delta = 0.25;
  166.         goodp = -1.0;
  167.         for(i=0; i<8; i++) {
  168.         tr = flerp(fr,lum,p);
  169.         tg = flerp(fg,lum,p);
  170.         tb = flerp(fb,lum,p);
  171.         if(simpcorone(tr,tg,tb,&r,&g,&b)) {
  172.             goodp = p;
  173.             p -= delta;
  174.         } else
  175.             p += delta;
  176.         delta = delta/2.0;
  177.         }
  178.         if(goodp < 0.0)
  179.         goodp = 1.0;
  180.         tr = flerp(fr,lum,goodp);
  181.         tg = flerp(fg,lum,goodp);
  182.         tb = flerp(fb,lum,goodp);
  183.         simpcorone(tr,tg,tb,&r,&g,&b);
  184.     }
  185.     } else {
  186.     simpcorone(fr,fg,fb,&r,&g,&b);
  187.     }
  188.     *cr = r;
  189.     *cg = g;
  190.     *cb = b;
  191. }
  192.  
  193. void corrow4(rbuf,gbuf,bbuf,n)
  194. short *rbuf;
  195. short *gbuf;
  196. short *bbuf;
  197. int n;
  198. {
  199.     float cr, cg, cb;
  200.     short lr, lg, lb;
  201.     short ir, ig, ib;
  202.  
  203.     lr = lg = lb = -1000;
  204.     while(n--) {
  205.         if((*rbuf != lr) || (*gbuf != lg) || (*bbuf != lb)) {
  206.             lr = *rbuf;
  207.             lg = *gbuf;
  208.             lb = *bbuf;
  209.             corone4(lr/255.0,lg/255.0,lb/255.0,&cr,&cg,&cb);
  210.             ir = cr*255.0+0.5;
  211.             ig = cg*255.0+0.5;
  212.             ib = cb*255.0+0.5;
  213.         }
  214.     *rbuf++ = ir;
  215.     *gbuf++ = ig;
  216.     *bbuf++ = ib;
  217.     }
  218. }
  219.  
  220. /* 
  221.  *    sproofing  stuff follows
  222.  *
  223.  */
  224. static float polycoef[3][4][3];
  225.  
  226. static setprinter()
  227. {
  228.     int i;
  229.  
  230.     for(i=0; i<3; i++) {
  231.     polycoef[CM][0][i] = ct[BLU][i]-ct[MAG][i]-ct[CYA][i]+1.0;
  232.     polycoef[CM][1][i] = ct[CYA][i]-1.0;
  233.     polycoef[CM][2][i] = ct[MAG][i]-1.0;
  234.     polycoef[CM][3][i] = 1.0;
  235.  
  236.     polycoef[MY][0][i] = ct[RED][i]-ct[YEL][i]-ct[MAG][i]+1.0;
  237.     polycoef[MY][1][i] = ct[MAG][i]-1.0;
  238.     polycoef[MY][2][i] = ct[YEL][i]-1.0;
  239.     polycoef[MY][3][i] = 1.0;
  240.  
  241.     polycoef[YC][0][i] = ct[GRE][i]-ct[CYA][i]-ct[YEL][i]+1.0;
  242.     polycoef[YC][1][i] = ct[YEL][i]-1.0;
  243.     polycoef[YC][2][i] = ct[CYA][i]-1.0;
  244.     polycoef[YC][3][i] = 1.0;
  245.     }
  246. }
  247.  
  248. static int firsted;
  249.  
  250. static pairtorgb(pair,c,m,k,r,g,b)
  251. int pair;
  252. float c, m, k;
  253. float *r, *g, *b;
  254. {
  255.     if(!firsted) {
  256.     setprinter();
  257.     firsted = 1;
  258.     }
  259.     k = 1.0-k;
  260.     *r = k*(c*m*polycoef[pair][0][0] + 
  261.           c*polycoef[pair][1][0] + 
  262.           m*polycoef[pair][2][0] + 1.0);
  263.     *g = k*(c*m*polycoef[pair][0][1] + 
  264.           c*polycoef[pair][1][1] + 
  265.           m*polycoef[pair][2][1] + 1.0);
  266.     *b = k*(c*m*polycoef[pair][0][2] + 
  267.           c*polycoef[pair][1][2] + 
  268.           m*polycoef[pair][2][2] + 1.0);
  269. }
  270.  
  271. void sproof4(r,g,b,sr,sg,sb)
  272. float r, g, b;
  273. float *sr, *sg, *sb;
  274. {
  275.     int c, m, y, k;
  276.     int ir, ig, ib;
  277.     float fc, fm, fy, fk;
  278.  
  279.     ir = 255.0*r;
  280.     ig = 255.0*g;
  281.     ib = 255.0*b;
  282.     rgb_to_cmyk(ir,ig,ib,&c,&m,&y,&k,0);
  283.     fc = c/255.0;
  284.     fm = m/255.0;
  285.     fy = y/255.0;
  286.     fk = k/255.0;
  287.     if(c == 0) 
  288.      pairtorgb(MY,fm,fy,fk,sr,sg,sb);
  289.     else if(m == 0) 
  290.      pairtorgb(YC,fy,fc,fk,sr,sg,sb);
  291.     else 
  292.      pairtorgb(CM,fc,fm,fk,sr,sg,sb);
  293. }
  294.  
  295. void sproofrow4(rbuf,gbuf,bbuf,n)
  296. short *rbuf;
  297. short *gbuf;
  298. short *bbuf;
  299. int n;
  300. {
  301.     float cr, cg, cb;
  302.     short lr, lg, lb;
  303.     short ir, ig, ib;
  304.  
  305.     lr = lg = lb = -1000;
  306.     while(n--) {
  307.         if((*rbuf != lr) || (*gbuf != lg) || (*bbuf != lb)) {
  308.             lr = *rbuf;
  309.             lg = *gbuf;
  310.             lb = *bbuf;
  311.             sproof4(lr/255.0,lg/255.0,lb/255.0,&cr,&cg,&cb);
  312.             ir = cr*255.0+0.5;
  313.             ig = cg*255.0+0.5;
  314.             ib = cb*255.0+0.5;
  315.         }
  316.     *rbuf++ = ir;
  317.     *gbuf++ = ig;
  318.     *bbuf++ = ib;
  319.     }
  320. }
  321.